﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeIt_.ServiceReference2;

namespace ShakeIt_
{
    public partial class createPage : PhoneApplicationPage
    {
        public createPage()
        {
            InitializeComponent();
        }

        public bool isFocused1 = false;
        public bool isFocused2 = false;
        public bool isFocused3 = false;

        private void title_GotFocus(object sender, RoutedEventArgs e)
        {
            if (isFocused1 == false)
            {
                titleBox.Text = "";
                isFocused1 = true;
            }
        }


        private void detail_GotFocus(object sender, RoutedEventArgs e)
        {
            if (isFocused3 == false)
            {
                detailBox.Text = "";
                isFocused3 = true;
            }
        }

        private void addressBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (isFocused2 == false)
            {
                addressBox.Text = "";
                isFocused2 = true;
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void commit_button_Click(object sender, RoutedEventArgs e)
        {
            string title = titleBox.Text;
            int type_index = typeBox.SelectedIndex;
            List<string> typelist = new List<string>();
            typelist.Add("类型");
            typelist.Add(type1.Text);
            typelist.Add(type2.Text);
            typelist.Add(type3.Text);
            string type = typelist[type_index];
            string detail = detailBox.Text;
            string address = addressBox.Text;
            DateTime bt = (DateTime)beginTimeOnlyBox.Value;
            DateTime bd = (DateTime)beginDateOnlyBox.Value;
            DateTime beginTime = new DateTime(bd.Year,bd.Month,bd.Day,bt.Hour,bt.Minute,bt.Second);
            DateTime et = (DateTime)endTimeOnlyBox.Value;
            DateTime ed = (DateTime)endDateOnlyBox.Value;
            DateTime endTime = new DateTime(ed.Year, ed.Month, ed.Day, et.Hour, et.Minute, et.Second);
           

            if (beginTime > endTime)
            {
                ErrorMessageBox.Text = "开始时间应该早于结束时间";
            }
            else if (type == "类型")
            {
                ErrorMessageBox.Text = "类型尚未选择";
            }
            else if (Data.userId == -1)
            {
                ErrorMessageBox.Text = "亲，发布活动请先登录哦！";
            }
            else
            {
                decimal longitude = (decimal)Data.currentLocation.Longitude;
                decimal latitude = (decimal)Data.currentLocation.Latitude;
                Data.proxy.InsertActivityCompleted += new EventHandler<InsertActivityCompletedEventArgs>(proxy_InsertActivityCompleted);
                Data.proxy.InsertActivityAsync(Data.userId, type, title, detail, longitude, latitude, address, beginTime, endTime);
            }
        }
        
        void proxy_InsertActivityCompleted(object sender, InsertActivityCompletedEventArgs e)
        {
            if (e.Result == "成功添加活动")
            {
                if (this.NavigationService.CanGoBack)
                {
                    this.NavigationService.GoBack();
                    return;
                }
            }
            ErrorMessageBox.Text = e.Result;
        }

        
    }
}